﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Web.SessionState;
namespace SZJT.Portal
{
    /// <summary>
    /// FZ_RandomCode1 的摘要说明
    /// </summary>
    public class RandomCode : IHttpHandler,IRequiresSessionState 
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Clear();
            if (context.Request["sid"] != null)
            {
                context.Response.ContentType = "text/plain";
                context.Response.Write(context.Session["RandomCode"]);
            }
            else
            {
                context.Response.ContentType = "image/jpeg";
                // 在此处放置用户代码以初始化页面
                //RndNum是一个自定义函数
                string VNum = RndNum(4); //这里的数字8代表显示的是8位的验证字符串！
                context.Session["RandomCode"] = VNum;
                //ValidateCode(VNum);
                context.Response.BinaryWrite(CreateValidateGraphic(VNum).ToArray());
            }
            context.Session[context.Session.SessionID] = context.Session["RandomCode"].ToString();
            context.Response.End();
        }
      

        //生成随机数函数中从Vchar数组中随机抽取
        //字母区分大小写
        public string RndNum(int VcodeNum)
        {
            string Vchar = "A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,W,X,Y,Z,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,W,X,Y,Z";
            //string Vchar = "零,壹,贰,叁,肆,伍,陆,柒,捌,玖,拾,湖,南,强,智,科,技,有,限,公,司,陈,伟,航,一,二,三,四,五,六,七,八,九,十,正" ;

            string[] VcArray = Vchar.Split(',');
            string VNum = "";//由于字符串很短，就不用StringBuilder了
            int temp = -1;//记录上次随机数值，尽量避免生产几个一样的随机数

            //采用一个简单的算法以保证生成随机数的不同
            Random rand = new Random();
            for (int i = 1; i < VcodeNum + 1; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
                }
                //int t =  rand.Next(35) ;
                int t = rand.Next(54);
                if (temp != -1 && temp == t)
                {
                    return RndNum(VcodeNum);
                }
                temp = t;
                VNum += VcArray[t];

            }
            return VNum;
        }

        public MemoryStream CreateValidateGraphic(string validateNum)
        {
            //Bitmap image=new Bitmap((int)Math.Ceiling(validateNum.Length*12.5),22);
            Bitmap image = new Bitmap(70, 20);
            Graphics g = Graphics.FromImage(image);
            try
            {
                //生成随机生成器
                Random random = new Random();
                //清空图片背景色
                g.Clear(Color.White);
                //画图片的干扰线
                for (int i = 0; i < 25; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }
                Font font = new Font("Arial", 14, (FontStyle.Bold | FontStyle.Italic));
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                g.DrawString(validateNum, font, brush, 3, 2);
                //画图片的前景干扰点
                for (int i = 0; i < 100; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);
                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }
                //画图片的边框线
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                //保存图片数据
                MemoryStream stream = new MemoryStream();
                image.Save(stream, ImageFormat.Jpeg);
                //输出图片
                return stream;
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}